package com.hhkj.study.admin.resource.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.resource.common.CommonValidateHandler;
import com.hhkj.study.admin.resource.entity.ResourceCourseOffline;
import com.hhkj.study.admin.resource.excel.ResourceCourseOfflineExcelRes;
import com.hhkj.study.admin.resource.model.podo.ResourceCourseOfflineDo;
import com.hhkj.study.admin.resource.model.podo.ResourceCourseOfflinePo;
import com.hhkj.study.admin.resource.model.req.ResourceCourseDocReq;
import com.hhkj.study.admin.resource.model.req.ResourceCourseOfflineReq;
import com.hhkj.study.admin.resource.model.res.ResourceCourseOfflineRes;
import com.hhkj.study.admin.resource.service.base.ResourceCourseOfflineService;
import com.hhkj.study.admin.resource.service.biz.ResourceCourseOfflineBizService;
import com.hhkj.study.admin.resource.service.extend.ResourceCourseOfflineExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.hhkj.study.common.model.res.ExcelImportResultRes;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
* ResourceCourseOfflineBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ResourceCourseOfflineBizServiceImpl extends BaseBizServiceImpl<ResourceCourseOffline, ResourceCourseOfflineReq, ResourceCourseOfflineRes, ResourceCourseOfflineDo, ResourceCourseOfflinePo, ResourceCourseOfflineService, ResourceCourseOfflineExtendService> implements ResourceCourseOfflineBizService {

    @Resource
    private GlobalCache globalCache;

    @Resource
    private CommonValidateHandler commonValidateHandler;


    @Override
    protected void afterPage(List<ResourceCourseOfflineRes> resourceCourseOfflineResList) {
        supplyCourseOffline(resourceCourseOfflineResList);
    }


    @Override
    protected void afterGetById(ResourceCourseOfflineRes resourceCourseOfflineRes) {
        supplyCourseOffline(Lists.newArrayList(resourceCourseOfflineRes));
    }

    @Override
    protected void afterList(List<ResourceCourseOfflineRes> resourceCourseOfflineRes) {
        supplyCourseOffline(resourceCourseOfflineRes);
    }

    @Override
    protected void beforeSave(ResourceCourseOfflineReq resourceCourseOfflineReq) {
        ResourceCourseOffline courseOffline = this.getExtendService().getByNameCourseType(resourceCourseOfflineReq.getName(), resourceCourseOfflineReq.getCourseType());
        if (ObjectUtil.isNotNull(courseOffline)) {
            throw new GlobalException(resourceCourseOfflineReq.getName() +"名称重复");
        }
    }

    @Override
    protected void beforeUpdate(ResourceCourseOfflineReq resourceCourseOfflineReq) {
        ResourceCourseOffline offline = this.getExtendService().getByIdNotNull(resourceCourseOfflineReq.getId());
        if (!offline.getName().equals(resourceCourseOfflineReq.getName())) {
            ResourceCourseOffline courseOffline = this.getExtendService().getByNameCourseType(resourceCourseOfflineReq.getName(), resourceCourseOfflineReq.getCourseType());
            if (ObjectUtil.isNotNull(courseOffline)) {
                throw new GlobalException(resourceCourseOfflineReq.getName() +"名称重复");
            }
        }
    }

    @Override
    public boolean updatePublishFlag(ResourceCourseDocReq req) {
        ResourceCourseOffline courseOffline = this.getExtendService().getByIdNotNull(req.getId());
        if (courseOffline.getPublishedFlag().equals(req.getPublishedFlag())) {
            throw new GlobalException(SysConst.Flag.IS.getFlag().equals(req.getPublishedFlag()) ?
                    "已上架" : "已下架");
        }
        LambdaUpdateWrapper<ResourceCourseOffline> offlineUpdate = new LambdaUpdateWrapper<>();
        offlineUpdate.eq(ResourceCourseOffline::getId, req.getId())
                .set(ResourceCourseOffline::getPublishedFlag, req.getPublishedFlag());
        return this.getService().update(offlineUpdate);
    }

    @Override
    public void export(ResourceCourseOfflineReq req, HttpServletResponse response) {
        List<ResourceCourseOfflineRes> offlineResList = list(req);
        if (CollectionUtil.isEmpty(offlineResList)) {
            throw new GlobalException("没有数据");
        }
        ExcelUtils.exportExcelRuoYi(response, BeanUtil.copyToList(offlineResList, ResourceCourseOfflineExcelRes.class), ResourceCourseOfflineExcelRes.class, "线下课列表", "线下课列表");
    }

    @Override
    public ExcelImportResultRes importOffline(MultipartFile file, HttpServletRequest request) {
        List<ResourceCourseOfflineExcelRes> offlineExcelList = ExcelUtils.importExcelRuoYi(file, ResourceCourseOfflineExcelRes.class, 1);
        if (CollectionUtil.isEmpty(offlineExcelList)) {
            throw new GlobalException("没有数据");
        }
        // 导入信息结果
        ExcelImportResultRes excelImportResultRes = commonValidateHandler.validate(offlineExcelList, (name, courseType) -> ObjectUtil.isNotNull(this.getExtendService().getByNameCourseType(name, courseType)));
        if (CollectionUtil.isEmpty(excelImportResultRes.getErrorList())) {
            this.getService().saveBatch(BeanUtil.copyToList(offlineExcelList, ResourceCourseOffline.class));
        }
        return excelImportResultRes;
    }

    /**
     * 补充线下课信息
     * @param resourceCourseOfflineResList
     */
    private void supplyCourseOffline(List<ResourceCourseOfflineRes> resourceCourseOfflineResList) {
        Map<Long, String> dictMap = globalCache.cacheDictMap(Lists.newArrayList(DictCodeEnum.COURSE_TYPE.getCode()));
        resourceCourseOfflineResList.forEach(resourceCourseOfflineRes -> resourceCourseOfflineRes.setCourseTypeName(dictMap.get(resourceCourseOfflineRes.getCourseType())));
    }
}